package ch.zbw.swt.sysventory.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import IMG.GetPath;

/*
 * GUI für die Darstellung der erfolgreich gescannten Computer
 */

public class GUIScanErfolgV2 extends JFrame {

	//Panel
	private JPanel panel;
	private JPanel panel_1;

	private JScrollPane scrollPane;

	// Labels
	private JLabel menuTitelLabel;

	//Bilder
	private ImageIcon goArrow;
	private ImageIcon backArrow;

	//Buttons
	private JButton go2DetailComputerButton;
	private JButton back2MenuButton;

	//Listener
	private Action action;

	//Funktionsvariablen
	private JTable table;
	private String[][] scannedCoumputersAndIPs;
	private String[] tableHeaders;

	
	/**
	 * Konstruktor für GUI
	 */
	public GUIScanErfolgV2(Action action) {
		this.action = action;

	}

	/**
	 * Konstruktor II für GUI mit vorauswahl PC
	 */
	
	public GUIScanErfolgV2(Action action, String[][] computers) {
		this.action = action;
		this.scannedCoumputersAndIPs = computers;
		this.tableHeaders = new String[] { "Computername", "IP-Adresse" };
	}

	/**
	 * initialisieren von GUI
	 */
	public void initialize() {
		
		//Eingenschaften von GUI
		
		this.getContentPane().setBackground(Color.WHITE);
		this.setBackground(Color.WHITE);
		this.setBounds(100, 100, 770, 480);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Einmittung
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int top = (screenSize.height - this.getHeight()) / 2;
		int left = (screenSize.width - this.getWidth()) / 2;
		
		//weitere Eingeschaften
		this.setLocation(left, top);
		this.setResizable(false);
		this.setVisible(true);

		//Einfügen der Elemente
		panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBounds(10, 11, 744, 83);
		this.getContentPane().add(panel);
		panel.setLayout(null);

		goArrow = new ImageIcon(GetPath.class.getResource("go.png"));
		backArrow = new ImageIcon(GetPath.class.getResource("back.png"));

		go2DetailComputerButton = new JButton(goArrow);
		go2DetailComputerButton.setBackground(Color.WHITE);
		go2DetailComputerButton.setBounds(592, 11, 129, 61);
		go2DetailComputerButton.setBorder(null);
		panel.add(go2DetailComputerButton);

		back2MenuButton = new JButton(backArrow);
		back2MenuButton.setBackground(Color.WHITE);
		back2MenuButton.setBounds(24, 11, 129, 61);
		back2MenuButton.setBorder(null);
		panel.add(back2MenuButton);

		menuTitelLabel = new JLabel("Scan Erfolgreich");
		menuTitelLabel.setFont(new Font("Impact", Font.PLAIN, 36));
		menuTitelLabel.setBounds(245, 11, 267, 61);
		panel.add(menuTitelLabel);

		panel_1 = new JPanel();
		panel_1.setBounds(10, 109, 744, 332);
		panel.add(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));

		scrollPane = new JScrollPane();
		panel_1.add(scrollPane, BorderLayout.CENTER);

		table = new JTable(scannedCoumputersAndIPs, tableHeaders);
		table.getColumnModel().getColumn(0).setPreferredWidth(88);
		table.getColumnModel().getColumn(1).setPreferredWidth(80);
		scrollPane.setViewportView(table);

		
		//Setzen der Listener
		go2DetailComputerButton.addActionListener(action);
		go2DetailComputerButton.setActionCommand("go2detailComputer");
		back2MenuButton.addActionListener(action);
		back2MenuButton.setActionCommand("back2Menu");

	}

	/*
	 * Liefert den auswählten Rechner aus in der JTable aus
	 */
	public String getSelectedcomputername() {
		int ausgewaehleReihe = table.getSelectedRow();

		if (ausgewaehleReihe >= 0) {
			String computername = (String) table
					.getValueAt(ausgewaehleReihe, 0);
			return computername;
		} else {
			return null;
		}

	}

}
